using System.Collections.Generic;
using UnityEngine;
using Verse;

namespace RimWorld;

[StaticConstructorOnStartup]
public class GeneGizmo_ResourceHemogen : GeneGizmo_Resource
{
	private static readonly Texture2D HemogenCostTex = SolidColorMaterials.NewSolidColorTexture(new Color(0.78f, 0.72f, 0.66f));

	private const float TotalPulsateTime = 0.85f;

	private List<Pair<IGeneResourceDrain, float>> tmpDrainGenes = new List<Pair<IGeneResourceDrain, float>>();

	public GeneGizmo_ResourceHemogen(Gene_Resource gene, List<IGeneResourceDrain> drainGenes, Color barColor, Color barhighlightColor)
		: base(gene, drainGenes, barColor, barhighlightColor)
	{
		draggableBar = true;
	}

	public override GizmoResult GizmoOnGUI(Vector2 topLeft, float maxWidth, GizmoRenderParms parms)
	{
		GizmoResult result = base.GizmoOnGUI(topLeft, maxWidth, parms);
		float num = Mathf.Repeat(Time.time, 0.85f);
		float num2 = 1f;
		if (num < 0.1f)
		{
			num2 = num / 0.1f;
		}
		else if (num >= 0.25f)
		{
			num2 = 1f - (num - 0.25f) / 0.6f;
		}
		if (((MainTabWindow_Inspect)MainButtonDefOf.Inspect.TabWindow)?.LastMouseoverGizmo is Command_Ability command_Ability && gene.Max != 0f)
		{
			foreach (CompAbilityEffect effectComp in command_Ability.Ability.EffectComps)
			{
				if (effectComp is CompAbilityEffect_HemogenCost compAbilityEffect_HemogenCost && compAbilityEffect_HemogenCost.Props.hemogenCost > float.Epsilon)
				{
					Rect rect = barRect.ContractedBy(3f);
					float width = rect.width;
					float num3 = gene.Value / gene.Max;
					rect.xMax = rect.xMin + width * num3;
					float num4 = Mathf.Min(compAbilityEffect_HemogenCost.Props.hemogenCost / gene.Max, 1f);
					rect.xMin = Mathf.Max(rect.xMin, rect.xMax - width * num4);
					GUI.color = new Color(1f, 1f, 1f, num2 * 0.7f);
					GenUI.DrawTextureWithMaterial(rect, HemogenCostTex, null);
					GUI.color = Color.white;
					return result;
				}
			}
			return result;
		}
		return result;
	}

	protected override string GetTooltip()
	{
		tmpDrainGenes.Clear();
		string text = $"{gene.ResourceLabel.CapitalizeFirst().Colorize(ColoredText.TipSectionTitleColor)}: {gene.ValueForDisplay} / {gene.MaxForDisplay}\n";
		if (gene.pawn.IsColonistPlayerControlled || gene.pawn.IsPrisonerOfColony)
		{
			text = ((!(gene.targetValue <= 0f)) ? (text + (string)("ConsumeHemogenBelow".Translate() + ": ") + gene.PostProcessValue(gene.targetValue)) : (text + "NeverConsumeHemogen".Translate().ToString()));
		}
		if (!drainGenes.NullOrEmpty())
		{
			float num = 0f;
			foreach (IGeneResourceDrain drainGene in drainGenes)
			{
				if (drainGene.CanOffset)
				{
					tmpDrainGenes.Add(new Pair<IGeneResourceDrain, float>(drainGene, drainGene.ResourceLossPerDay));
					num += drainGene.ResourceLossPerDay;
				}
			}
			if (num != 0f)
			{
				string text2 = ((num < 0f) ? "RegenerationRate".Translate() : "DrainRate".Translate());
				text = text + "\n\n" + text2 + ": " + "PerDay".Translate(Mathf.Abs(gene.PostProcessValue(num))).Resolve();
				foreach (Pair<IGeneResourceDrain, float> tmpDrainGene in tmpDrainGenes)
				{
					text = text + "\n  - " + tmpDrainGene.First.DisplayLabel.CapitalizeFirst() + ": " + "PerDay".Translate(gene.PostProcessValue(0f - tmpDrainGene.Second).ToStringWithSign()).Resolve();
				}
			}
		}
		if (!gene.def.resourceDescription.NullOrEmpty())
		{
			text = text + "\n\n" + gene.def.resourceDescription.Formatted(gene.pawn.Named("PAWN")).Resolve();
		}
		return text;
	}
}
